<?php

namespace App\Admin\Renderable;

use App\Models\AdminUser as AdminUserModel;
use App\Support\Data;
use Dcat\Admin\Support\LazyRenderable;
use Dcat\Admin\Widgets\Table;

class WorkerListTable extends LazyRenderable
{
    public function render()
    {
        // 获取ID
        $id = $this->key;

        // 获取其他自定义参数
        $worker_user_id = $this->worker_user_id;


        $data = AdminUserModel::with(['roles'])
            ->whereIn('id', explode(',', $worker_user_id))
            ->get(['username', 'name', 'employee_number', 'region', 'id']) //['username', 'name', 'body', 'created_at']
            ->toArray();

        $result = [];
        $arr = Data::getRegion();
        $res = [];
        foreach ($data as $key => $val) {
            $result['username'] = $val['username'];
            $result['name'] = $val['name'];
            $result['employee_number'] = $val['employee_number'];
            $result['region'] = isset($arr[$val['region']]) ? $arr[$val['region']] : ' - ';
            if (isset($val['roles'][0]['is_external'])) {
                if ($val['roles'][0]['is_external'] == 1) {
                    $result['roles'] = '客户 - ' . $val['roles'][0]['name'];
                } else {
                    $result['roles'] = '公司 - ' . $val['roles'][0]['name'];
                }
                array_push($res, $result);
            } else {
                $result['roles'] = '未知角色';
                array_push($res, $result);
            }
        }
        // dd($res);
        $titles = [
            '用户名',
            '名称',
            '员工编号',
            '区域',
            '身份'
        ];

        return Table::make($titles, $res);
    }
}
